//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.base.domain.LiveBarrage;
import com.ruoyi.live.base.domain.LiveBarrageChannel;
import com.ruoyi.live.base.domain.LiveTaskUserChannel;
import com.ruoyi.live.base.mapper.LiveBarrageChannelMapper;
import com.ruoyi.live.base.mapper.LiveBarrageMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserChannelMapper;
import com.ruoyi.live.tool.commons.ICommons;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LiveBarrageService {
    private static final Logger log = LoggerFactory.getLogger(LiveBarrageService.class);
    @Autowired
    LiveBarrageMapper dao;
    @Autowired
    LiveTaskUserChannelMapper channelDao;
    @Autowired
    LiveBarrageChannelMapper lbcDao;
    @Autowired
    CacheService cacheService;

    public LiveBarrageService() {
    }

    @Async
    public void addOneBarrage(Long id, Integer timeIndex, LiveBarrage lb, int addCount, int level, String userName) {
        if ("1".equals(this.cacheService.getParam("SYSTEM_INSERT_BARRAGE"))) {
            LiveTaskUserChannel channel = (LiveTaskUserChannel) this.channelDao.selectById(id);
            if (channel != null) {
                LiveBarrageChannel lbc = (new LiveBarrageChannel()).setChannelId(id).setType(lb.getType()).setBarrageId(lb.getId()).setTimeIndex(timeIndex).setFromUserName(userName).setFromUserLevel(level).setUserAddCount(addCount).setCreateTime(System.currentTimeMillis());
                this.lbcDao.insert(lbc);
            }
        }

    }

    public void addLiveBarrage(LiveBarrage entity) throws Exception {
        try {
            entity.setCreateTime(System.currentTimeMillis());
            entity.setCreateUser(SessionInfo.getUserId());
            entity.setState(1);
            this.dao.insert(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("新增LiveBarrage失败,编号LiveBarrageService-addLiveBarrage-1");
            throw new Exception("新增失败,编号LiveBarrageService-addLiveBarrage-1");
        }
    }

    public void updateLiveBarrage(LiveBarrage entity) throws Exception {
        try {
            this.dao.updateById(entity);
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("更新LiveBarrage失败,编号LiveBarrageService-updateLiveBarrage-1,id:{}", entity.getId());
            throw new Exception("更新失败,编号LiveBarrageService-updateLiveBarrage-1");
        }
    }

    public LiveBarrage getLiveBarrage(Long id) {
        return (LiveBarrage) this.dao.selectById(id);
    }

    public int deleteLiveBarrage(Long[] ids) {
        UpdateWrapper<LiveBarrage> updateWrapper = new UpdateWrapper<LiveBarrage>();
        updateWrapper.in("id", ids).set("state", 0);
        int count = this.dao.update(new LiveBarrage(), updateWrapper);
        return count;
    }

    public List<LiveBarrage> query(LiveBarrage entity) {
        QueryWrapper<LiveBarrage> queryWrapper = new QueryWrapper<LiveBarrage>();
        queryWrapper.eq(!ICommons.isNullOrEmpty(entity.getId()), "id", entity.getId())
                .eq(!ICommons.isNullOrEmpty(entity.getState()), "state", entity.getState())
                .eq(!ICommons.isNullOrEmpty(entity.getCreateTime()), "create_time", entity.getCreateTime())
                .eq(!ICommons.isNullOrEmpty(entity.getCreateUser()), "create_user", entity.getCreateUser())
                .eq(!ICommons.isNullOrEmpty(entity.getType()), "type", entity.getType())
                .eq("state", 1)
                .like(!ICommons.isNullOrEmpty(entity.getContent()), "content", entity.getContent());
        return this.dao.selectList(queryWrapper);
    }
}
